package com.liang.maogouyuyin.ui.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.liang.maogouyuyin.bean.WeightBean
import com.liang.maogouyuyin.local.LocalRepository
import kotlinx.coroutines.launch

class WeightViewModel: ViewModel() {
    private val _weightList = MutableLiveData<ArrayList<WeightBean>>().apply { value = ArrayList() }
    val weightList get() = _weightList

    fun saveWeight(name: String, weight: Float, date: Long) {
        val bean = WeightBean(
            name, weight, date
        )
        viewModelScope.launch {
            LocalRepository.saveWeight(bean)
        }
    }

    fun deleteWeight(bean: WeightBean) {
        viewModelScope.launch {
            LocalRepository.deleteWeight(bean)
        }
    }

    fun updateWeight(bean: WeightBean) {
        viewModelScope.launch {
            LocalRepository.updateWeight(bean)
        }
    }

    fun loadWeightByName(name: String) {
        viewModelScope.launch {
            _weightList.value = LocalRepository.loadWeight(name)
        }
    }
}